package com.heisen.base.exception;

import com.heisen.base.constant.HttpConstant;
import enums.ApiReturnEnums;
import lombok.Data;

/**
 * @author sen
 * @description  通用异常
 * @date 2023/12/27 9:53
 */
@Data
public class RRException extends RuntimeException{

    private Integer code = null;

    private String msg = null;

    private static RRException plEx= new RRException();

    public RRException(String msg) {
        code = 0;
        setMsg(msg);
    }

    public RRException() {
    }

    public RRException(Integer ecode, String emsg) {
        this.code = ecode;
        this.msg = emsg;
    }


    public RRException(ApiReturnEnums apiReturnEnums) {
        super(apiReturnEnums.getDesc());
        this.code = apiReturnEnums.getCode();
        this.msg = apiReturnEnums.getDesc();
    }


    /**
     *
     * @param msg
     * @param formatstr
     * @return
     */
    public static RRException rrExceptionformatByCode(Integer ecode,String msg,Object ...formatstr){
        plEx.setCode( ecode);
        plEx.setMsg(String.format(msg, formatstr) );
        return plEx;
    }

    /**
     *   %s  转换
     * @param msg
     * @param formatstr
     * @return
     */
    public static RRException rrException(String msg,Object ...formatstr){
        plEx.setCode( HttpConstant.HttpCode.SC_FAILE);
        plEx.setMsg(String.format(msg, formatstr) );
        return plEx;
    }




    @Override
    public String getMessage() {
        return msg;
    }
}
